package com.cn.dim.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.dim.pojo.Pages;
import com.cn.dim.pojo.Prop;
import com.cn.dim.service.PropService;
import com.github.pagehelper.Page;


@Controller
@RequestMapping("/prop")
public class PropController {

	@Resource
	private PropService propService;
	@RequestMapping(value ="/queryProp",method = RequestMethod.GET)
	@ResponseBody
	//Pages是自己定义的页面实体�?
	public Map<String,Object> queryProp(HttpServletRequest request,Prop p,Pages pages,String stime,String etime){
		Map<String,Object> param=new HashMap<>();//定义查询参数
		Map<String,Object> reMap=new HashMap<>();//定义返回值，其中有一项必须是集合，先按照下面的写法
		//参数p是列表页中以查询条件数据临时组成的过渡prop对象
		param.put("pageIndex", pages.getPage());
		param.put("pageSize", pages.getRows());
		param.put("prop", p);
		
		if(stime!=null&&!stime.equals(""))
		{
			stime=stime+" 00:00:00";
		}
		if(etime!=null&&!etime.equals(""))
		{
			etime=etime+" 23:59:59";
		}
		param.put("stime", stime);//开始时间
		param.put("etime", etime);//截止时间
		
		try {
			Page<Map<String,Object>> rList = (Page)propService.queryProp(param);
			//Page对象认为是一个以键值对保存的一行记录的多条记录的集合，
			for(Map m:rList){
				
				Map<String,Object> queryProptypenamepara=new HashMap<>();//定义queryAuth方法查询参数
				queryProptypenamepara.put("proptypeid", m.get("proptypeid"));

				List<Map<String,Object>> proptypenames=propService.queryProptypeName(queryProptypenamepara);//返回值的List中只有一个Map元素
				String proptypenameStr="";
				for(Map proptypename:proptypenames){//实际上只循环一遍
					proptypenameStr=proptypenameStr+(String)proptypename.get("proptypename");
				}
				m.put("proptypenameStr", proptypenameStr);//在每行记录中补充1个用于显示的键值对
			}
			System.out.println(rList);
			reMap.put("rows", rList);
			reMap.put("total", rList.getTotal());
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	//宣传类别（板块）下拉框获取函数；
	@RequestMapping(value ="/queryProptype",method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String,Object>> queryProptype(HttpServletRequest request){
		Map<String,Object> param=new HashMap<>();//查询不需要参数，这里作为一个空参数传过去；
		Map<String,Object> reMap=new HashMap<>();
		List<Map<String,Object>> list = new ArrayList<>();
		try {
			list =propService.queryProptype(param);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return list;
	}

	@RequestMapping(value ="/insertProp",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> insertProp(Prop p)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		String propid=com.cn.dim.utils.StringUtil.makeUUID();
		p.setId(propid);
		p.setIsdel("0");
		
		//这里补充创建人的id的代码
		
		try 
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String cdate_s = sdf.format(new Date());
			Date cdate = sdf.parse(cdate_s);//创建日期
			p.setCreatetime(cdate);
			p.setReletime(cdate_s);
			
			int flag = propService.insertProp(p);
			if(flag>0)
			{
				reMap.put("message", "插入成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/updateProp",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> updateProp(Prop p,String propid)  
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		p.setId(propid);//实体对象传来的并不含有id的值，这里要使用页面传过来propid值进行填充
		
		//这里补充修改（删除）人的id的代码
		try 
		{
//			String content =  java.net.URLDecoder.decode(p.getContent(), "utf-8");
//			System.out.println(content.length());
//			p.setContent(content);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String cdate_s = sdf.format(new Date());
			Date cdate = sdf.parse(cdate_s);//修改（删除）日期
			p.setUpdatetime(cdate);
			p.setReletime(cdate_s);
			
			int flag = propService.updateProp(p);
			if(flag>0)
			{
				reMap.put("message", "修改成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}

	@RequestMapping(value ="/delProp",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> delProp(HttpServletRequest request,String propid)   //插入�?条单�?的prop记录
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
        Map<String,Object> m=new HashMap<>();//构造service接口中delProp方法的参数
        m.put("propid", propid);
		
		//这里补充修改（删除）人的id的代码
		
		try 
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date cdate = sdf.parse(sdf.format(new Date()));//修改（删除）日期
			m.put("updatetime", cdate);
			
			int flag = propService.delProp(m);
			if(flag>0)
			{
				reMap.put("message", "删除成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/queryPropById",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> queryPropById(HttpServletRequest request,String propid){
		Map<String,Object> m=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		m.put("propid", propid);
		try {
			Prop prop =propService.queryPropById(m);
			reMap.put("data", prop);
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
}
